﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using FrameWork.Setting;
using MyServer.Common;
using MyServer.TcpIP;
using ScreenMonitor.Common;
using ScreenMonitor.Common.Objects;
using ScreenMonitor.Common.File;

namespace ScreenMonitor
{
    /// <summary>
    /// Tcp协议相关命令执行中心
    /// </summary>
    [SettingParentDefine]
    public class TcpCommandCenter : DataOperatorBase
    {

        public event OperateCommandEventHandler OperateCommand;

        #region========公有函数========

        private object runCommandLock = new object();

        private object sendCommandLock = new object();
        public void SendCommand(string command, TcpClient client)
        {
            lock (sendCommandLock)
            {
                CommandObjects.TcpCenter.SendData(client, new Byte[] { 0 }, Util.GetpkgName(command), true);
            }
        }

        public void SendCommandWithData(string command, byte[] data, TcpClient client)
        {
            lock (sendCommandLock)
            {
                CommandObjects.TcpCenter.SendData(client, data, Util.GetpkgName(command), true);
            }
        }

        public bool SendCommandToAll(List<TcpClient> Clients, string command)
        {
            lock (sendCommandLock)
            {
                foreach (TcpClient c in Clients)
                {
                    SendCommand(command, c);
                }
                return true;
            }
        }

        public bool SendCommandToAllWithData(List<TcpClient> Clients, string command, byte[] data)
        {
            lock (sendCommandLock)
            {
                foreach (TcpClient c in Clients)
                {
                    SendCommandWithData(command, data, c);
                }
                return true;
            }
        }

        #endregion

        #region========构造函数========

        protected override bool Operate(ref DataPackageCollection pkg, out DataPackageCollection RePkg)
        {
            lock (runCommandLock)
            {
                RePkg = null;
                if(OperateCommand!=null)
                {
                    OperateCommand(this, new OperateCommandEventArgs(pkg));
                }
                return true;
            }
        }

        public TcpCommandCenter(DataCenterBase Center)
            : base(Center)
        { }

        #endregion

    }
}
